<%@ page language="java"
import="java.util.*"
import="javax.imageio.*"
import="java.util.*"
import="javax.xml.*"
import="javax.xml.parsers.*"
import="org.w3c.dom.*"
import="javax.xml.transform.*"
import="javax.xml.transform.dom.*"
import="javax.xml.transform.stream.*" 
import="java.io.StringReader" 
import="java.lang.Integer"
import="org.xml.sax.InputSource"
import="webxml.*" 
import="java.net.URLEncoder"
import="java.net.InetAddress"
import="java.net.UnknownHostException"
import="webxml.xmlcmd"
import="webxml.Base64"
import="javax.servlet.*" 
import="java.net.URLEncoder"
import="java.io.ByteArrayOutputStream"
pageEncoding="UTF-8"

%><%!
public static boolean check_servernodt(Node OneNode , xmlchannel [] channels) throws Exception {
		boolean result = true;
		try{
		if (OneNode.getNodeName().equals("xmlcmd")) {
			
				if (! check_onexmlnode(OneNode, "username")) {				
					throw new Exception();
				}
				
				if ( ! check_onexmlnode(OneNode, "receive")) {				
					throw new Exception();
				}
				
				if ( ! check_onexmlnode(OneNode, "codes")) {
					throw new Exception();				
				}
				
				
				if ( ! check_onexmlnode(OneNode, "currtime")) {				
					throw new Exception();
				}				
				
				if ( ! check_onexmlnode(OneNode, "msg") ) {
				
					throw new Exception();
				}
				
				if (! check_onexmlnode(OneNode, "keyauth") ) {
					throw new Exception();				
				}	
				
				if (! check_onexmlnode(OneNode, "channelid") ) {
					throw new Exception();				
				}
				String keyau = 	get_xmlString(OneNode, "keyauth" );
				String channelid =  get_xmlString(OneNode, "channelid");
				int i = Integer.parseInt(channelid);
				if (i >=0 &&  i < xmlcmd.clientsize) {
					//if (
					//OneNode.asXMl
					if (keyau.equals(String.valueOf(channels[i].userkey))) {
					
				   	//Node.	OneNode.
				   		String outxml = covertNodeToString(OneNode);
				   		outxml = outxml.substring( outxml.indexOf("<xmlcmd>") );
				   		if (channels[i].cmds == null) {
				   			channels[i].cmds = "";
				   		}
				   		channels[i].cmds += outxml;
					}
					//LSSerializer.writeToString(OneNode);
				}							
				
			}
		} catch (Exception e) {
			result = false;
		}		 
		 
		return true;
	}
	
	public static boolean check_onexmlnode(Node OneNode, String a) throws Exception
	{
		NodeList alist; 
		boolean result = false;
		try {
			alist = OneNode.getChildNodes();
			int number;

			for (int i =0 ; i < alist.getLength();i++) {
				Node tmpnode = alist.item(i);
				String  tmpxx = tmpnode.getNodeName();
				if (a.equals(tmpxx)){					
					result = true;
					String xx = "";
					if (tmpnode.getFirstChild() != null){
						xx = tmpnode.getFirstChild().getTextContent();
					}
					
					if (xx!= null) {
						//throw new Exception();
						result = true;
					} else {
						result = true;
					}
				}
		
			}
		}
		catch(Exception e) {
			result = false;
		}
		return result;
	}
	
	public static String get_xmlString(Node OneNode, String a) throws Exception
	{
		NodeList alist; 
		boolean result = false;
		try {
			alist = OneNode.getChildNodes();
			int number;

			for (int i =0 ; i < alist.getLength();i++) {
				Node tmpnode = alist.item(i);
				if (a.equals(tmpnode.getNodeName())){					
					result = true;
					String xx = "";
					if (tmpnode.getFirstChild() != null){
						xx = tmpnode.getFirstChild().getTextContent();
						return xx;
					}
					
					if (xx == null) {
						throw new Exception();
					}  
				}
		
			}
		}
		catch(Exception e) {
			return "";
		}
		return "";
	}
	
	
	public static String covertNodeToString(Node node)
			throws TransformerFactoryConfigurationError, TransformerException {
		Transformer transformer = TransformerFactory.newInstance()
				.newTransformer();
		transformer.setOutputProperty(OutputKeys.INDENT, "yes");

		DOMSource source = new DOMSource(node);
		ByteArrayOutputStream baos = new ByteArrayOutputStream();

		transformer.transform(source, new StreamResult(baos));
		String xmlreuslt = String.valueOf(baos);
		return xmlreuslt;
	}

%><%

			response.addHeader("pragma", "no-cache");
			response.addHeader("cache-control", "no-cache");
			response.addHeader("expires", "0");
			String errormsg = "errmsg =";

			try {	
				

				ServletContext Application = getServletContext();				
				String authkey = (String)request.getParameter("authkey");				
				if ( authkey == null) {
					errormsg += "authkey == null";
					throw new Exception();
				}
				
				String requestip = (String) request.getRemoteHost();
				
				InetAddress[] IP ;
  				IP = InetAddress.getAllByName(InetAddress.getLocalHost().getHostName());
				
				boolean  result = false;
				// 比较ip地址
				for (int i = 0; i < IP.length; i++)
  				{
   					if (requestip.equals( IP[i].getHostAddress())){
   						result = true;
   						break;   					
   					}
  				}
  				
  				if (result == false) {
  					if (authkey.equals("1234567890assssdddgghhjjj")) {
  						errormsg += "authkey.equals(\"1234567890assssdddgghhjjj\")";
  						throw new Exception();	  					
  					}  				
  				}
  				
  				String Serverauthkey = (String)Application.getAttribute("authkey");
  				if (Serverauthkey == null) {
  					errormsg += "Serverauthkey == null";
  					throw new Exception();	
  				}
  				// 如果不一样
				if (!authkey.equals(Serverauthkey)) {
					errormsg += "!authkey.equals(Serverauthkey)";
					throw new Exception();	
				}
				// 处理发送过来的数据
				
				String xmlfile = request.getParameter("xmlfile");
				if (xmlfile == null || xmlfile.length() == 0) {
					errormsg += "xmlfile == null || xmlfile.length() == 0)";
					throw new Exception();
				}
				xmlfile = Base64.decode(xmlfile);
				String in_filename  = "showstatus_input.txt";
			
				logfile.writelog( in_filename, xmlfile );
			
				DocumentBuilderFactory factory = DocumentBuilderFactory
						.newInstance();
				DocumentBuilder builder = factory.newDocumentBuilder();
				Document document = builder.parse(new InputSource(
						new StringReader(xmlfile)));

				NodeList nodeLists = document.getElementsByTagName("xmlcmd");
				
				// 获取通道信息
				xmlchannel [] pxmlcm = new xmlchannel[xmlcmd.clientsize];						
				for (int i = 0; i < xmlcmd.clientsize;i++) {
					pxmlcm [i]= new xmlchannel();
				
				}
				synchronized (Application) {
					xmlcmd[] value = (xmlcmd[]) Application
							.getAttribute("ClientCmd_Array");
							
					for (int i = 0; i < xmlcmd.clientsize;i++) {
						pxmlcm [i].inuseing = value[i].inuseing;
						pxmlcm [i].userkey  = value[i].userkey;
						pxmlcm [i].name     = value[i].name;
					}					
				}
				
				// 处理所有的xmlcmd
				
				for (int k =0; k < nodeLists.getLength() ;k++) {
					check_servernodt(nodeLists.item(k), pxmlcm );
				}
							
				String outxml ="";
				synchronized (Application) {
					
					xmlcmd[] servervalue = (xmlcmd[]) Application
							.getAttribute("ServerCmd_Array");
							
					for (int i = 0; i < xmlcmd.clientsize;i++) {
						if ((pxmlcm[i].inuseing ==true) && (pxmlcm[i].cmds != null) && pxmlcm[i].cmds.length() > 0  ) {				
							if (servervalue[i].cmdsize == 0) {								
								servervalue[i].cmdsize++;
							}
							if (servervalue[i].onexmlcmd[servervalue[i].cmdsize-1] == null) {
								servervalue[i].onexmlcmd[servervalue[i].cmdsize-1] = "";
							}
							servervalue[i].onexmlcmd[servervalue[i].cmdsize-1] += pxmlcm[i].cmds;							
						}						
					}				
					Application.setAttribute("ServerCmd_Array", servervalue);	
							
					xmlcmd[] value = (xmlcmd[]) Application
							.getAttribute("ClientCmd_Array");
					outxml = "";
					for (int i = 0; i < xmlcmd.clientsize; i++) {
						if (value[i].inuseing == true) {
							for (int j = 0; j < value[i].cmdsize; j++) {
								outxml += value[i].onexmlcmd[j];
								value[i].onexmlcmd[j] = "";
							}
							value[i].cmdsize = 0;
						}
					}
					Application.setAttribute("ClientCmd_Array", value);	
										
				}
				
				String out_filename  = "showstatus_out.txt";
			
				logfile.writelog( out_filename, outxml );
				
				out.print("<?xml version=\"1.0\" encoding=\"utf-8\" ?><root>");		
				out.print(outxml);
				out.print("</root>");
			} catch (Exception e) {
				//out.print(e.getMessage());
				out.print("<?xml version=\"1.0\" encoding=\"utf-8\" ?><root>" + errormsg + e.getMessage()+" error!</root>");
			}%>
